package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.TigDevice;
import org.jeecg.modules.service.DeviceService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @Description: 同步定时任务测试
 *
 * 此处的同步是指 当定时任务的执行时间大于任务的时间间隔时
 * 会等待第一个任务执行完成才会走第二个任务
 *
 *
 * @author: jianxing
 * @date: 2022年09月18日
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class DeviceAutoConnectJob implements Job {

    @Autowired
    DeviceService deviceService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private RabbitMqClient rabbitMqClient;

    public static boolean isInitialise = true;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //log.info(" --- 同步任务调度开始 --- ");
        try {
            if(isInitialise) {
                deviceService.resetDeviceState();
                isInitialise = false;
            }
            List<TigDevice> deviceList = deviceService.getActiveDevices();
            //激活设备发送到Mq进行连接
            for(int i=0;i<deviceList.size();i++){
                BaseMap bm = new BaseMap();
                bm.put("deviceId",deviceList.get(i).getId());
                rabbitMqClient.sendMessage("deviceReConnect",bm);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //测试发现 每5秒执行一次
        //log.info(" --- 执行完毕，时间："+ DateUtils.now()+"---");
    }

}
